package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.bean.MsChgPlanBean;
import com.koron.css2.meterService.bean.MsChgPlanDetailBean;
import com.koron.css2.meterService.dto.MsChgPlanDetailDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 换表回填
 * @author 周利斌
 *
 */
public class MsChgBackfill implements ServerInterface{
	
	private static Logger logger = LogManager.getLogger(MsChgBackfill.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
		try {
			List<MsChgPlanDetailBean> msChgPlanDetails = JsonUtils.objectToList(req.getData(), MsChgPlanDetailBean.class);
			MsChgPlanDetailMapper mapper = factory.getMapper(MsChgPlanDetailMapper.class);	
			MsChgPlanMapper msChgPlanMapper = factory.getMapper(MsChgPlanMapper.class);
			UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
			MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
			//换表生成变更事项
			ChangeUserMapper cmapper = factory.getMapper(ChangeUserMapper.class);
			int iCount = 0;
			MsChgPlanBean msChgPlan = null;
			
			for(MsChgPlanDetailBean msChgPlanDetail : msChgPlanDetails) {
				if(msChgPlan == null) {
					msChgPlan = msChgPlanMapper.selectMsChgPlan(msChgPlanDetail.getReceiptId());
				}
				if(StringUtils.isBlank(msChgPlanDetail.getOldMeterNo()))  {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "旧水表编号不能为空", Void.class);
				}
				if(msChgPlan.getReceiptType() == 1 && StringUtils.isBlank(msChgPlanDetail.getNewMeterNo()))  {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "新水表编号不能为空", Void.class);
				}
				if(StringUtils.isBlank(msChgPlanDetail.getReceiptId()))  {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "单据编号不能为空", Void.class);
				}
				String oldConcentratorNo="";
				MsInfoDto msInfo = msInfoMapper.getMsInfoByNo(msChgPlanDetail.getNewMeterNo());
				MsInfoDto oldMsInfo = msInfoMapper.getMsInfoByNo(msChgPlanDetail.getOldMeterNo());
				if(msInfo != null) {
					msChgPlanDetail.setNewFactoryNo(msInfo.getFactoryNo());
					msChgPlanDetail.setNewMeterCert(msInfo.getMeterCert());
				}else {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, msChgPlanDetail.getNewMeterNo()+"新水表不存在", Void.class);
				}
				if (oldMsInfo!=null){
					msChgPlanDetail.setOldFactoryNo(oldMsInfo.getFactoryNo());
				}else {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, msChgPlanDetail.getOldMeterNo()+"旧水表不存在", Void.class);
				}
				if(msChgPlanDetail.getOldMeterNum() != null) {
					UserBean user = userMapper.getUserInfoByUserNo(msChgPlanDetail.getUserNo(), UserBean.USER);
					oldConcentratorNo = user.getConcentratorNo();
					msChgPlanDetail.setMargin(msChgPlanDetail.getOldMeterNum() - user.getLastReadingNum());
				}
				if(msChgPlanDetail.getOldMeterBore() == null  ) {
					MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
					MeterBoreBean meterBore =  meterBoreMapper.selectMeterBoreById(oldMsInfo.getMeterBore());
					msChgPlanDetail.setOldMeterBore(meterBore.getBoreName());
				}
				MsChgPlanDetailDto ms = mapper.selectByOldMeterNo(msChgPlanDetail.getOldMeterNo());
				if(StringUtils.isBlank(ms.getNewMeterNo())) {   //是否是第一次回填
					iCount = msChgPlan.getBackfillAmount() + 1;
					msChgPlanMapper.updateBackfillAmount(msChgPlan.getId(), iCount);  //修改回填数量
				}else {
					iCount = msChgPlan.getBackfillAmount();
				}

				iCount = mapper.updateByOldMeterNo(msChgPlanDetail); //回填换表信息
				if (iCount > 0){
					msInfoMapper.updateMsInfoStatus(msChgPlanDetail.getNewMeterNo(), 3); // 在途
					logger.info("水表编号{}状态置为在途", msChgPlanDetail.getNewMeterNo());
				}

				//获取变更事项
				List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
				//水表表身码
				if(!msChgPlanDetail.getOldFactoryNo().equals(msChgPlanDetail.getNewFactoryNo())) {
					UserChangeItemBean bean = new UserChangeItemBean();
					bean.setReceiptId(msChgPlan.getId());
					bean.setReceiptType("ms_chg_plan#1");
					bean.setUserNo(msChgPlanDetail.getUserNo());
					bean.setBillNo(msChgPlan.getBillNo());
					bean.setCreateInfo(userInfo);
					bean.setChangeItem("水表表身码");
					bean.setOldItemValue(msChgPlanDetail.getOldFactoryNo());
					bean.setNewItemValue(msChgPlanDetail.getNewFactoryNo());
					item.add(bean);
				}
				// 水表口径
				if(!msChgPlanDetail.getOldMeterBore().equals(msChgPlanDetail.getNewMeterBore())) {
					UserChangeItemBean bean = new UserChangeItemBean();
					bean.setReceiptId(msChgPlan.getId());
					bean.setReceiptType("ms_chg_plan#1");
					bean.setUserNo(msChgPlanDetail.getUserNo());
					bean.setBillNo(msChgPlan.getBillNo());
					bean.setCreateInfo(userInfo);
					bean.setChangeItem("水表口径");
					bean.setOldItemValue(msChgPlanDetail.getOldMeterBore());
					bean.setNewItemValue(msChgPlanDetail.getNewMeterBore());
					item.add(bean);
				}
				UserChangeItemBean userChangeItem = new UserChangeItemBean();
				userChangeItem.setReceiptId(msChgPlan.getId());
				userChangeItem.setReceiptType("ms_chg_plan#1");
				userChangeItem.setUserNo(msChgPlanDetail.getUserNo());
				userChangeItem.setBillNo(msChgPlan.getBillNo());
				userChangeItem.setCreateInfo(userInfo);
				userChangeItem.setChangeItem("最后抄码");
				userChangeItem.setOldItemValue(String.valueOf(msChgPlanDetail.getUpperNum()));
				userChangeItem.setNewItemValue(String.valueOf(msChgPlanDetail.getNewMeterNum()));
				item.add(userChangeItem);
				//换表日期
				UserChangeItemBean replaceDateItem = new UserChangeItemBean();
				replaceDateItem.setReceiptId(msChgPlan.getId());
				replaceDateItem.setReceiptType("ms_chg_plan#1");
				replaceDateItem.setUserNo(msChgPlanDetail.getUserNo());
				replaceDateItem.setBillNo(msChgPlan.getBillNo());
				replaceDateItem.setCreateInfo(userInfo);
				replaceDateItem.setChangeItem("换表日期");
				replaceDateItem.setOldItemValue("空");
				replaceDateItem.setNewItemValue(DateUtils.parseDate(msChgPlanDetail.getReplaceDate(), "yyyy-MM-dd"));
				item.add(replaceDateItem);
				//查看集中器是否改变
				if(!StringUtils.equalsAny(oldConcentratorNo, msChgPlanDetail.getConcentratorNo())) {
					UserChangeItemBean bean = new UserChangeItemBean();
					bean.setReceiptId(msChgPlan.getId());
					bean.setReceiptType("ms_chg_plan#1");
					bean.setUserNo(msChgPlanDetail.getUserNo());
					bean.setBillNo(msChgPlan.getBillNo());
					bean.setCreateInfo(userInfo);
					bean.setChangeItem("集中器编号");
					bean.setOldItemValue(oldConcentratorNo);
					bean.setNewItemValue(msChgPlanDetail.getConcentratorNo());
					item.add(bean);
				}
				//插入到变更事项临时表 1:正式表 2：临时表
				cmapper.userChangeItemAdd(item, 2);
			}
			info.setData(iCount);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("修改失败");
			logger.error(e.getMessage(), e);
		}
		return info;
	}
	
}
